<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class AdminUser extends Model
{
    protected $table = 'admin_users';//表名
    protected $fillable = [''];//白名单


    public static function login($username, $password)
    {
        $user = self::check($username);
        if ($user) {
            $user = $user->toArray();
            $status = passwordVerify($password, $user['password']);
            unset($user['password']);

            if ($status) {
//
                return ['status' => true, 'message' => '登陆成功', 'data' => $user];

            } else {
                return ['status' => false, 'message' => '密码不正确', 'data' => []];
            }
        } else {
            return ['status' => false, 'data' => $user, 'message' => '用户不存在'];
        }
    }

    public static function check($username)
    {
        $user = self::where(['username' => $username])->first();
        return $user;
    }

    public static function register($username, $password)
    {
        $user = self::check($username);
        if ($user) {
            return ['status' => false, 'message' => '该用户已存在'];
        } else {
            $model = self::create(['username' => $username, 'password' => passwordEncode($password)]);
            if ($model->save()) {
                return ['status' => true, 'message' => '创建成功'];
            } else {
                return ['status' => false, 'message' => '创建失败'];
            }
        }
    }


}